/**
 * @author harry
 * @date 2021/12/6/20:23
 */

package com.ui.utils;

import com.pojo.Contact;

public class ContactFilter implements MyFilter {
    private String name;
    private String phone;
    private String email;
    private String address;
    private int statusCode;

    public int checkParam(Object obj) {
        assert obj instanceof Contact;
        Contact contact = (Contact) obj;
        name = contact.getName();
        phone = contact.getPhone();
        email = contact.getEmail();
        address = contact.getAddress();
        if(checkName() != 0) return statusCode;
        if(checkPhone() != 0) return statusCode;
        if(checkEmail() != 0) return statusCode;
        if(checkAddress() != 0) return statusCode;
        return 0;
    }

    /**
     * 名字格式检验：非空、长度小于20
     */
    private int checkName() {
        if(name.length() == 0) return statusCode = 1;
        if(name.length() > 20) return statusCode = 2;
        return 0;
    }

    /**
     * 电话号码格式检验：全数字、长度小于20
     */
    private int checkPhone() {
        if(!phone.matches("[0-9]*")) return statusCode = 3;
        if(phone.length() == 0) return statusCode = 4;
        if(phone.length() > 20) return statusCode = 5;
        return 0;
    }

    /**
     * 地址格式检验：空 or 长度在40以内
     */
    private int checkAddress() {
        if(address.length() > 40) return statusCode = 8;
        return 0;
    }

    /**
     * 邮箱格式检验：空 or 长度小于30
     */
    private int checkEmail() {
        if(email.length() > 30) return statusCode = 7;
        return 0;
    }

}
